﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for RoleDAL
/// </summary>
public class RoleDAL: StandardConnectDB, IRoles
{
	public RoleDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<Roles> GetAllRoles()
    {
        //Create object listRoles
        List<Roles> listRoles = new List<Roles>();
        
        //Get all Roles from database
        DataTable dt = this.DTExecuteSelect("SELECT * FROM Roles");

        //Add Roles to list roles
        foreach (DataRow dr in dt.Rows)
        { 
            //Convert object DataRow to object Roles and add to list roles
            listRoles.Add(ConvertDataRowToObjectRole(dr));
            
        }

        return listRoles;
    }

    public Roles GetRoleById(int idRole)
    {
        //Craete object Roles
        Roles role = new Roles();

        //Create param list store parameter execute store procedure
        Dictionary<String, Object> paramList = new Dictionary<String, Object>();
        //Add param pIdRole to paramList
        paramList.Add("@pIdRole", idRole);

        //Get Role from database by store procedure
        DataTable dtRoles = this.DTExecuteSelect("procGetRoleById", paramList);
        
        //Create object DataRow store data first's row in DataTable
        DataRow dr = dtRoles.Rows[0];
        
        //Convert object DataRow to object Roles
        role = this.ConvertDataRowToObjectRole(dr);
        
        return role;
    }

    public bool CreateRole(string roleName)
    {
        //Create object result
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();
        //Add param to list
        paramList.Add("@pRoleName", roleName);

        //Insert object Role to table Roles in database by store procedure
        result = this.ExecuteUpdate("procCreateRoles", paramList);

        return result;
    }

    public bool UpdateRole(Roles role)
    {
        //Create object result
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();
        //Add param to list
        paramList.Add("@pRoleName", role.RoleName);
        paramList.Add("@pIdRole", role.IdRole);

        //Update object Roles by store procedure
        result = this.ExecuteUpdate("procUpdateRoles", paramList);

        return result;
    }

    public bool DeletedRole(int idRole)
    {
        //Create object result
        bool result = false;

        //Create param list store parameter execute store procedure
        Dictionary<string, object> paramList = new Dictionary<string, object>();
        //Add param to list
        paramList.Add("@pIdRole", idRole);

        //Update object Roles by store procedure
        result = this.ExecuteUpdate("procDeleteRoles", paramList);

        return result;
    }


    public Roles ConvertDataRowToObjectRole(DataRow dr)
    {
        Roles role = new Roles();

        role.IdRole = int.Parse(dr["IdRole"].ToString());
        role.RoleName = dr["RoleName"].ToString();

        return role;
    }
}